package com.fulihui.wgj.schedule.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.util.RpcResultCheckUtil;
import com.fulihui.wgj.facade.dto.WxMassMsgLogDTO;
import com.fulihui.wgj.facade.request.mass.CalcMassMsgRequest;
import com.fulihui.wgj.facade.service.mass.SendWxMassService;
import com.fulihui.wgj.facade.service.mass.WxMassLogService;
import com.fulihui.wgj.schedule.annotation.MutexLock;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 微信图文群发定时任务
 *
 * @author wang_wx
 * @date 2018-08-02
 */
@Component
public class WxMassSendMsgJob {

    private static final Logger LOGGER = LoggerFactory.getLogger(WxMassSendMsgJob.class);

    @Reference(version = "1.0.0")
    WxMassLogService wxMassLogService;

    @Reference(version = "1.0.0")
    SendWxMassService sendWxMassService;

    @MutexLock(node = "${job.node.wxMassSendMsgJob_execute}")
    @Scheduled(cron = "${job.cron.wxMassSendMsgJob_execute}")
    public void execute() {
        LOGGER.info("WxMassSendMsgJob start");
        SimpleResult<List<WxMassMsgLogDTO>> simpleResult = wxMassLogService.queryMassLogs4Job();
        LOGGER.info("massLogResult 4JOB = {}", simpleResult);
        if (RpcResultCheckUtil.isSuccess(simpleResult)) {
            List<WxMassMsgLogDTO> massMsgLogDTOS = simpleResult.getData();
            if (CollectionUtils.isEmpty(massMsgLogDTOS)) {
                LOGGER.info("there is no mass send task !");
                return;
            }
            massMsgLogDTOS.stream().forEach(wxMassMsgLogDTO -> {
                Date jobTime = wxMassMsgLogDTO.getJobTime();
                // 定时任务时间到了 进行发送
                if (Objects.nonNull(jobTime) && jobTime.getTime() <= System.currentTimeMillis()) {
                    LOGGER.info("mass job wxMassMsgLogDTO = {}", wxMassMsgLogDTO);
                    String sendCondition = wxMassMsgLogDTO.getSendCondition();
                    if (StringUtils.isNotBlank(sendCondition)) {
                        CalcMassMsgRequest calcMassMsgRequest = JSON.parseObject(sendCondition, CalcMassMsgRequest.class);
                        calcMassMsgRequest.setRecordId(wxMassMsgLogDTO.getId());
                        BaseResult baseResult = sendWxMassService.sendMassMsg4Job(calcMassMsgRequest);
                        if (baseResult.isSuccess()) {
                            LOGGER.info("mass job recordId = {}执行成功！", wxMassMsgLogDTO.getId());
                        } else {
                            LOGGER.info("mass job recordId = {}执行失败！", wxMassMsgLogDTO.getId());
                        }
                    }
                }
            });
            LOGGER.info("WxMassSendMsgJob end");
        }
    }

}
